package cn.itcast.bos.web.action;

import javax.ws.rs.core.MediaType;

import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import cn.itcast.bos.domain.base.Area;
import cn.itcast.bos.domain.constant.Constants;
import cn.itcast.bos.domain.take_delivery.Order;
import cn.itcast.bos.web.action.common.BaseAction;
import cn.itcast.crm.domain.Customer;

@Namespace("/")
@ParentPackage("struts-default")
@Controller
@Scope("prototype")
public class OrderAction extends BaseAction<Order> {

	private static final long serialVersionUID = 1L;

	// 接受发件人和收件人的区域信息
	private String sendAreaInfo;
	private String recAreaInfo;

	public void setSendAreaInfo(String sendAreaInfo) {
		this.sendAreaInfo = sendAreaInfo;
	}

	public void setRecAreaInfo(String recAreaInfo) {
		this.recAreaInfo = recAreaInfo;
	}

	@Action(value = "order_save", results = { @Result(name = "success", location = "index.html", type = "redirect") })
	public String save() {

		// 根据sendAreaInfo查找Area并且封装
		String[] sendAreaArr = sendAreaInfo.split("/");
		//此时的发件人区域是瞬时态的
		Area sendArea = new Area();
		sendArea.setProvince(sendAreaArr[0]);
		sendArea.setCity(sendAreaArr[1]);
		sendArea.setDistrict(sendAreaArr[2]);
		model.setSendArea(sendArea);
		// 根据recAreaInfo查找Area并且封装
		String[] recAreaArr = recAreaInfo.split("/");
		//此时的收件人区域是瞬时态的
		Area recArea = new Area();
		recArea.setProvince(recAreaArr[0]);
		recArea.setCity(recAreaArr[1]);
		recArea.setDistrict(recAreaArr[2]);
		model.setRecArea(recArea);
		// 订单关联客户
		Customer existCustomer = (Customer) ServletActionContext.getRequest().getSession()
				.getAttribute("existCustomer");
		model.setCustomer_id(existCustomer.getId());

		// 调用webService 将order传递给bos_management
		WebClient.create(Constants.BOS_MANAGEMENT_URL + "/services/orderService/order/save").type(MediaType.APPLICATION_JSON)
				.post(model);
		
		return SUCCESS;
	}
}
